package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private IEmployeeService employeeService;
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Autowired
    public void setEmployeeService(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }


    // 处理保存的请求 /employee/saveOrUpdate
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() == null) {
            employeeService.save(employee, ids);
        } else {
            employeeService.update(employee, ids);
        }
        return new JsonResult();
    }

    // 处理去保存的请求 /employee/input
    // 处理去修改的请求 /employee/input
    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        // 把所有部门数据存到 model 中
        model.addAttribute("departments", departmentService.listAll());
        // 把所有角色数据存到 model 中
        model.addAttribute("roles", roleService.listAll());

        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
        }
        return "employee/input";
    }


    // 处理删除请求 /employee/delete
    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    // 处理查询的请求 /employee/list
    @RequestMapping("/list")
    @RequiredPermission(name = "员工页面", expression = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        int i = 1/0;
        model.addAttribute("pageInfo", employeeService.query(qo));
        // 把所有部门数据存到 model 中
        model.addAttribute("departments", departmentService.listAll());


        return "employee/list"; // /WEB-INF/views/employee/list.jsp
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();
        //查询数据库是否存在该用户名
        Employee employee = employeeService.selectByUsername(username);
        map.put("valid", employee == null); //验证不通过返回false ,验证通过返回true
        return map;
    }

    @RequiredPermission(name = "员工导出", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //指定文件名
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        //把数据输出给浏览器(响应流)
        workbook.write(response.getOutputStream());
    }


    @RequiredPermission(name = "员工导入", expression = "employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }

    @ResponseBody
    @RequestMapping("/updatePwd")
    public JsonResult updatePwd(String oldPassword, String newPassword) {
        employeeService.updatePassword(oldPassword, newPassword);
        return new JsonResult();
    }

    @RequestMapping("/reset")
    public String reset(Long id){
        employeeService.reset(id);
        return "redirect:/employee/list";
    }
    @RequiredPermission(name = "员工禁用", expression = "employee:setStatus")
    @RequestMapping("/setStatus")
    @ResponseBody
    public JsonResult setStatus(Long id){
        employeeService.setStatus(id);
       return new JsonResult();
    }


}
